package com.smforj.ssm.mina.parser;

import java.io.UnsupportedEncodingException;

import com.smforj.ssm.mina.codec.MsgUtils;
import com.smforj.ssm.mina.message.MessageBody;
import com.smforj.ssm.mina.message.MessageHeader;
import com.smforj.ssm.mina.message.MessageModel;


public class JParserImpl implements JParser {

	public Object StringToObject(String msg) { 
		MessageModel model = new MessageModel();
		
		byte[] src = msg.getBytes();//MsgUtils.stringToBytes(msg); 
		//12,4,8,20,20,4,4,16,4,4,20,4,4,4,52
		//消息头(只处理需要数据)
		MessageHeader header = new MessageHeader();
		int aindex = 0; 
		int len = 12;
		//头信息 标记
		byte[] dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		header.setFlag(MsgUtils.bytesToString(dest).trim());
		aindex += len; 
		
		//版本
		len = 4;
		/***  未处理 ***/
		aindex += len;
		//设备类型
		len = 8;
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		header.setDeviceType(MsgUtils.bytesToString(dest).trim());
		aindex += len;  
		//站点父节点编号
		len = 20;
		/***  未处理 ***/
		aindex += len; 
		//站点子节点编号
		len = 20;
		/***  未处理 ***/
		aindex += len; 
		//设备通道类型
		len = 4;
		/***  未处理 ***/
		aindex += len;  
		//设备编号 
		len = 4;
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		header.setDeviceNo(MsgUtils.bytesToInt(dest));
		aindex += len;  
		//设备IP: 
		len = 16;
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		header.setDeviceIp(MsgUtils.bytesToString(dest).trim());
		aindex += len;  
		//比对总结果（警告类型）
		len = 4;
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		header.setWarnType(MsgUtils.bytesToInt(dest));
		aindex += len;
		//人工处理
		len = 4;
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		header.setManual(MsgUtils.bytesToInt(dest)); 
		aindex += len;
		
		//采集时间
		len = 20;
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		header.setDate(MsgUtils.bytesToString(dest).trim());
		aindex += len;  
		//消息体长度 
		len = 4;
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		header.setMsgLength(MsgUtils.bytesToInt(dest));
		aindex+=len;
		//人脸比对结果 
		len = 4;
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		header.setFaceiRes(MsgUtils.bytesToInt(dest));
		aindex+=len;
		//指纹比对结果 
		len = 4;
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		header.setHandiRes(MsgUtils.bytesToInt(dest));
		aindex+=len; 
		//预留
		len = 52;
		/***  未处理 ***/
		aindex += len;   
		
		//消息体从180开始 
		MessageBody body = new MessageBody();
		//身份证信息 
		//姓名
		len = 30;
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		String name = encodeCNStr(dest);
		/*
		try {
			name = new String(dest, "GB2312");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		body.setName(name);//(MsgUtils.bytesToString(dest).trim());
		aindex +=len;
		//性别
		len = 4;
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		String sex = encodeCNStr(dest);
		body.setSex(sex);
		aindex +=len;
		//nation 民族
		len = 10;
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		String nation = encodeCNStr(dest);
		body.setNation(nation);
		aindex += len; 
		  
		//Birthday
		len = 30;
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		String birthday = encodeCNStr(dest);
		body.setBirthday(birthday);
		aindex += len; 

		//身份证
		len = 36;
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		body.setIdenNum(MsgUtils.bytesToString(dest).trim()); 
		aindex += len; 
		
		//Address
		len = 70;
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		String address = encodeCNStr(dest);
		body.setAddress(address);
		aindex += len; 
		//Statday
		len = 30;
		/*** 身份证有效期 ***/
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		String statday = encodeCNStr(dest);
		body.setStatday(statday);
		aindex += len; 
		//Endday
		len = 30;
		/*** 身份证有效期 ***/
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		String endday = encodeCNStr(dest);
		body.setEndday(endday);
		aindex += len;  
		//Department
		len = 30;
		/*** 发证机关 ***/
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		String depart = encodeCNStr(dest);
		body.setDepartment(depart);
		aindex += len;  
		//Reserved
		len = 32;
		/*** 身份证信息 ***/
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		String Reserved = encodeCNStr(dest);
		body.setReserved(Reserved);
		aindex += len;   
		//身份证图片 长度
		len = 4;
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		body.setIdImgLength(MsgUtils.bytesToInt(dest));
		aindex += len;
		//身份证图片
		len = body.getIdImgLength();
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		body.setIdImg(dest);  
		aindex += len;
		//采集的指纹图片 长度
		len = 4;
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		body.setFpImgLength(MsgUtils.bytesToInt(dest));
		aindex += len;
		//采集的指纹图片  
		//有则处理
		if(body.getFpImgLength() > 0)
		{
			len = body.getFpImgLength();
			dest = new byte[len];
			System.arraycopy(src, aindex, dest, 0, dest.length);
			body.setFpImg(dest);
			aindex += len;
		} 
		
		//USB采集图片（JPG） 长度
		len = 4;
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		body.setCapImgLength(MsgUtils.bytesToInt(dest));
		aindex += len;
		//USB采集图片（JPG） 
		//有则处理
		if(body.getCapImgLength() > 0)
		{
			len = body.getCapImgLength();
			dest = new byte[len];
			System.arraycopy(src, aindex, dest, 0, dest.length);
			body.setCapImg(dest);
			aindex += len;
		}
		
		//USB剪裁的采集图片（JPG）
		len = 4;
		dest = new byte[len];
		System.arraycopy(src, aindex, dest, 0, dest.length);
		body.setCropImgLength(MsgUtils.bytesToInt(dest));
		aindex += len;
		//USB采集图片（JPG） 
		//有则处理
		if(body.getCropImgLength() > 0)
		{
			len = body.getCropImgLength();
			dest = new byte[len];
			System.arraycopy(src, aindex, dest, 0, dest.length);
			body.setCropImg(dest);
			aindex += len;
		} 
		model.setHeader(header);
		model.setBody(body);
		
		return model;
	}

	public String ObjectToString(Object obj) {
		// TODO Auto-generated method stub
		return null;
	}
	
	private String encodeCNStr(byte[] dest) {
		String name = null;
		try {
			name = new String(dest, "GB2312");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return name;
	}
	
	public static void main(String[] arg0)
	{ 
		int index = 0;
		String str = "dfsdfsdfd73895784fdsfs";
		System.out.println(str.substring(0,index+=9));
		System.out.println(str.substring(index,index+=8));
	}

}
